package cn.action;

import cn.base.BaseAction;
import cn.domain.Department;
import cn.domain.PageBean;
import cn.domain.User;
import cn.utils.DepartmentUtils;
import cn.utils.HQLHelper;
import com.opensymphony.xwork2.util.ValueStack;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import sun.jvm.hotspot.debugger.Page;

import java.util.List;
import java.util.Set;

/**
 * Created by jiaxiang Huang on 2017/3/9.
 */
@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department> {
    private Long parmentId;

    public Long getParmentId() {
        return parmentId;
    }

    public void setParmentId(Long parmentId) {
        this.parmentId = parmentId;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public String findAll() {
        if (parmentId == null) {
            //查询顶级部门列表
//            List<Department> list=departmentService.Top();
//            getValueStack().set("list",list);
            HQLHelper hqlHelper = new HQLHelper(Department.class);
            hqlHelper.addWhere("parentId is null");
            PageBean pageBean = departmentService.getPageBean(hqlHelper, getCurrentPage());
            getValueStack().set("list", pageBean.getRecordList());
            getValueStack().push(pageBean);
        } else {
//            List<Department> list=departmentService.Down(parmentId);
            HQLHelper hqlHelper = new HQLHelper(Department.class);
            hqlHelper.addWhere("parent.id = " + parmentId);
            PageBean pageBean = departmentService.getPageBean(hqlHelper, getCurrentPage());
            Department dept = departmentService.getById(parmentId);
            getValueStack().set("list", pageBean.getRecordList());
            getValueStack().push(pageBean);
            getValueStack().set("dept", dept);
        }/*
        List<Department> list=departmentService.findAll();
        getValueStack().set("list",list);*/
        return "list";
    }

    /**
     * 根据id删除
     *
     * @return
     */
    public String delete() {
        departmentService.delete(model.getId());
        return "toList";
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    public String add() {
        model.setId(0L);
        //查询所有部门
        List<Department> list = departmentService.Top();
        List<Department> treeList = DepartmentUtils.getTreeList(list, null);
        getValueStack().set("list", treeList);
        return "add";
    }

    /**
     * 添加数据
     *
     * @return
     */
    public String save() {
        if (model.getId() == 0) {
            //添加数据
            adds();
        } else {
            Update();
        }
        return "toList";
    }

    /**
     * 跳转至修改页面
     *
     * @return
     */
    public String update() {

        //根据id查询
        Department department = departmentService.getById(model.getId());

        //查询所有部门
        List<Department> list = departmentService.Top();
        List<Department> treeList = DepartmentUtils.getTreeList(list, null);

        //压栈
        getValueStack().set("list", treeList);

        //回显数据
        getValueStack().push(department);
        if (department.getParent() != null) {
            parmentId = department.getParent().getId();
        }
        return "add";
    }


    public String updetas() {
        return "toList";
    }


    //添加数据方法
    private void adds() {

        if (parmentId != 0) {

            //根据id查询
            Department department = departmentService.getById(parmentId);

            model.setParent(department);
            departmentService.save(model);
        } else {
            model.setParent(null);
            departmentService.save(model);
        }
    }

    private void Update() {

        //根据id查询
        Department department = departmentService.getById(parmentId);

        if (department != null) {
            model.setParent(department);
        } else {
            model.setParent(null);
        }
        model.setDescription(model.getDescription());
        model.setName(model.getName());
        departmentService.update(model);
    }
}
